﻿var ConnectorControl, NodeControl,
  __hasProp = {}.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

NodeControl = (function(_super) {

  __extends(NodeControl, _super);

  function NodeControl(attrs) {
    var circle, cx, cy, text,
      _this = this;
    this.attrs = attrs;
    this.attrs.width = 160;
    this.attrs.height = 120;
    this.connectors = [];
    NodeControl.__super__.constructor.call(this, this.attrs);
    this.rect = new Kinetic.Rect({
      x: this.x,
      y: this.y,
      width: this.width,
      height: this.height,
      fill: "white",
      stroke: "violet",
      strokeWidth: 1
    });
    this.group.add(this.rect);
    cx = this.x + this.width / 2;
    cy = this.y + this.height / 2;
    this.connectCircle = new Kinetic.Circle({
      x: cx,
      y: cy,
      radius: 50,
      stroke: "gray",
      strokeWidth: 1
    });
    this.group.add(this.connectCircle);
    circle = new Kinetic.Circle({
      x: cx,
      y: cy,
      radius: 35,
      stroke: "violet",
      strokeWidth: 1,
      draggable: false
    });
    this.group.add(circle);
    circle = new Kinetic.Circle({
      x: cx,
      y: cy,
      radius: 10,
      fill: "violet",
      strokeWidth: 0
    });
    this.group.add(circle);
    text = new Kinetic.Text({
      x: cx,
      y: cy,
      text: this.text,
      fontSize: 10,
      fontFamily: "Tahoma",
      fontStyle: 'bold',
      textFill: "red",
      align: "center",
      verticalAlign: "middle"
    });
    this.group.add(text);
    this.group.on("mouseover", function() {
      if (!_this.selected) {
        _this.group.children[0].setStroke('blue');
      }
      return _this.layer.draw();
    });
    this.group.on("mouseout", function() {
      if (!_this.selected) {
        _this.group.children[0].setStroke('violet');
        return _this.layer.draw();
      }
    });
  }

  NodeControl.prototype.setSelected = function(selected) {
    this.selected = selected;
    NodeControl.__super__.setSelected.call(this, this.selected);
    if (this.selected) {
      return this.group.children[0].setStroke('red');
    } else {
      return this.group.children[0].setStroke('violet');
    }
  };

  NodeControl.prototype.typeName = 'NodeControlEx';

  return NodeControl;

})(UserControl);

ConnectorControl = (function(_super) {

  __extends(ConnectorControl, _super);

  function ConnectorControl(attrs) {
    var circle, mainShape, ref, _ref,
      _this = this;
    this.attrs = attrs;
    ConnectorControl.__super__.constructor.call(this, this.attrs);
    this.x1 = this.attrs.x1;
    this.y1 = this.attrs.y1;
    this.x2 = this.attrs.x2;
    this.y2 = this.attrs.y2;
    this.node1 = this.attrs.node1;
    this.node2 = this.attrs.node2;
    ref = this;
    mainShape = new Kinetic.Shape({
      drawFunc: function() {
        var brect, context;
        context = this.getContext();
        context.lineWidth = this.attrs.strokeWidth;
        context.strokeStyle = this.attrs.stroke;
        context.beginPath();
        context.moveTo(ref.x1, ref.y1);
        context.lineTo(ref.x2, ref.y2);
        context.closePath();
        context.stroke();
        context.lineWidth = 1;
        context.strokeStyle = 'transparent';
        brect = GeometryHelper.makeSimpleBound({
          x: ref.x1,
          y: ref.y1
        }, {
          x: ref.x2,
          y: ref.y2
        }, this.attrs.strokeWidth + 4);
        context.beginPath();
        context.moveTo(brect.x1, brect.y1);
        context.lineTo(brect.x2, brect.y2);
        context.lineTo(brect.x3, brect.y3);
        context.lineTo(brect.x4, brect.y4);
        context.closePath();
        return context.stroke();
      },
      stroke: "violet",
      strokeWidth: 3,
      name: 'BoundRect'
    });
    mainShape.on('mouseover', function() {});
    this.group.add(mainShape);
    circle = new Kinetic.Circle({
      x: this.x1,
      y: this.y1,
      radius: 5,
      stroke: "violet",
      strokeWidth: 0,
      fill: "violet",
      listening: false
    });
    this.group.add(circle);
    circle = new Kinetic.Circle({
      x: this.x2,
      y: this.y2,
      radius: 5,
      stroke: "violet",
      strokeWidth: 0,
      fill: "violet",
      draggable: true,
      listening: false
    });
    this.group.add(circle);
    this.creatingCircle = new Kinetic.Circle({
      x: this.x2,
      y: this.y2,
      radius: 8,
      stroke: "violet",
      strokeWidth: 1,
      draggable: true,
      listening: false
    });
    this.setCreatingMode = function(creating) {
      var item, _i, _j, _len, _len1, _ref, _ref1, _results, _results1;
      this.creating = creating;
      if (this.creating) {
        this.group.add(this.creatingCircle);
        _ref = this.group.children;
        _results = [];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          item = _ref[_i];
          _results.push(item.attrs.listening = false);
        }
        return _results;
      } else {
        if ((this.group.children.indexOf(this.creatingCircle)) >= 0) {
          this.group.remove(this.creatingCircle);
        }
        _ref1 = this.group.children;
        _results1 = [];
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          item = _ref1[_j];
          _results1.push(item.attrs.listening = true);
        }
        return _results1;
      }
    };
    this.setPos2 = function(x, y) {
      this.x2 = x;
      this.y2 = y;
      this.group.children[2].setX(x);
      this.group.children[2].setY(y);
      if (this.creating) {
        this.group.children[3].setX(x);
        this.group.children[3].setY(y);
      }
      return this.layer.draw();
    };
    this.setPos1 = function(x, y) {
      this.x1 = x;
      this.y1 = y;
      this.group.children[1].setX(x);
      this.group.children[1].setY(y);
      return this.layer.draw();
    };
    this.updatePositionByNodes = function() {
      var cPos1, cPos2, points;
      if (this.node1 === void 0 || this.node2 === void 0) {
        return;
      }
      cPos1 = this.node1.connectCircle.getAbsolutePosition();
      cPos2 = this.node2.connectCircle.getAbsolutePosition();
      points = GeometryHelper.getJoinedPointsOfCircleAndLine1(cPos1.x, cPos1.y, this.node1.connectCircle.getRadius(), cPos2.x, cPos2.y);
      this.setPos1(points[0], points[1]);
      points = GeometryHelper.getJoinedPointsOfCircleAndLine1(cPos2.x, cPos2.y, this.node1.connectCircle.getRadius(), cPos1.x, cPos1.y);
      return this.setPos2(points[0], points[1]);
    };
    this.setCreatingMode((_ref = this.attrs.creating) != null ? _ref : false);
    mainShape.on('mouseover', function() {
      var item, _i, _len, _ref1;
      if (_this.selected) {
        return;
      }
      _ref1 = _this.group.children;
      for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
        item = _ref1[_i];
        item.attrs.stroke = 'blue';
      }
      return _this.layer.draw();
    });
    mainShape.on('mouseout', function() {
      var item, _i, _len, _ref1;
      if (_this.selected) {
        return;
      }
      _ref1 = _this.group.children;
      for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
        item = _ref1[_i];
        item.attrs.stroke = 'violet';
      }
      return _this.layer.draw();
    });
  }

  ConnectorControl.prototype.setSelected = function(selected) {
    var item, _i, _j, _len, _len1, _ref, _ref1, _results, _results1;
    this.selected = selected;
    ConnectorControl.__super__.setSelected.call(this, this.selected);
    if (this.selected) {
      _ref = this.group.children;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        item = _ref[_i];
        _results.push(item.attrs.stroke = 'red');
      }
      return _results;
    } else {
      _ref1 = this.group.children;
      _results1 = [];
      for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
        item = _ref1[_j];
        _results1.push(item.attrs.stroke = 'violet');
      }
      return _results1;
    }
  };

  ConnectorControl.prototype.typeName = 'ConnectorControlEx';

  return ConnectorControl;

})(UserControl);
